using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using RideMe.Presentation.Properties;
using RideMe.Core;
using RideMe.AddIns;
using RideMe.AddIns.Interfaces;
using ICSharpCode.TextEditor.Added;
using System.IO;

namespace RideMe.Presentation
{
    public partial class OptionsDialog : DialogBase
    {
        #region Node Tag Constants 

        private const string GeneralOptions = "general";
        private const string TextEditorOptions = "text_editor";
        private const string SyntaxHighligthing = "syntax_highlighting";
        private const string AddIns = "add-ins";

        #endregion

        /// <summary>
        /// Initializes a new instance of the <see cref="T:OptionsDialog"/> class.
        /// </summary>
        public OptionsDialog()
        {
            InitializeComponent();
            
            optionsTreeView.Nodes[0].Expand();
            optionsTreeView.SelectedNode = optionsTreeView.Nodes[0].Nodes[0];

            LoadOptions();

            LoadAddInOptions();
        }

        private void LoadOptions()
        {
            generalOptionsPanel.RubyPath = Options.Instance.RubyPath;
            generalOptionsPanel.WebServerPort = Options.Instance.ServerPort;
            generalOptionsPanel.LineEndings = Options.Instance.LineEndings;
            generalOptionsPanel.LoadLastProjectOnStartUp = Options.Instance.LoadLastProjectAtStartUp;
            generalOptionsPanel.LaunchInternalBrowser = Options.Instance.LaunchInternalBrowser;
			generalOptionsPanel.RunScriptServer = Options.Instance.RunServer;

            textEditorOptionsPanel.ShowLineNumbers = Options.Instance.ShowLineNumbers;
            textEditorOptionsPanel.EditorFontFamily = Options.Instance.EditorFont.FontFamily.Name;
            textEditorOptionsPanel.EditorFontSize = Options.Instance.EditorFont.Size;
			textEditorOptionsPanel.ConvertTabsToSpaces = Options.Instance.ConvertTabsToSpaces;
            textEditorOptionsPanel.EnableFolding = Options.Instance.EnableFolding;
            textEditorOptionsPanel.ModelColor = Options.Instance.ModelColor;
            textEditorOptionsPanel.ViewColor = Options.Instance.ViewColor;
            textEditorOptionsPanel.ControllerColor = Options.Instance.ControllerColor;
            textEditorOptionsPanel.TestsColor = Options.Instance.TestsColor;
            textEditorOptionsPanel.ShowTabs = Options.Instance.ShowTabs;
            textEditorOptionsPanel.ShowSpaces = Options.Instance.ShowSpaces;
            textEditorOptionsPanel.ShowEOL = Options.Instance.ShowEOL;

        }

        private void LoadAddInOptions()
        {
            if (AddInService.LoadedAddIns.Count > 0)
            {
                TreeNode addinsNode = optionsTreeView.Nodes.Add("Add-Ins");
                addinsNode.Tag = AddIns;
                foreach (IAddIn addin in AddInService.LoadedAddIns)
                {
                    TreeNode addinNode = addinsNode.Nodes.Add(addin.Name);
                    addinNode.Tag = addin;
                }
            }
        }

        /// <summary>
        /// Handles the AfterSelect event of the optionsTreeView control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="T:System.Windows.Forms.TreeViewEventArgs"/> instance containing the event data.</param>
        private void optionsTreeView_AfterSelect(object sender, TreeViewEventArgs e)
        {
            if (optionsTreeView.SelectedNode.Tag == null) return;

            switch (optionsTreeView.SelectedNode.Tag.ToString())
            {
                case GeneralOptions :
                    generalOptionsPanel.BringToFront();
                    break;
                case TextEditorOptions :
                    textEditorOptionsPanel.BringToFront();
                    break;
                case SyntaxHighligthing :
                    highlightingOptionsPanel1.BringToFront();
                    break;
                case AddIns:
                    break;
                default:
                    //See if we clicked an add-in option
                    if (optionsTreeView.SelectedNode.Parent.Tag != null && optionsTreeView.SelectedNode.Parent.Tag.ToString() == AddIns)
                    {
                        //If so, update the add-in options panel with the selected add-in, and ensure the add-in options are brought to the front to see
                        addInOptionsPanel.SetActiveAddIn((IAddIn)optionsTreeView.SelectedNode.Tag);
                        addInOptionsPanel.BringToFront();

                        RideMe.AddIns.Base.ApplicationContext.Current.UserInterfaceEvents.OnOptionsControlCreated((IAddIn)optionsTreeView.SelectedNode.Tag,
                            new RideMe.AddIns.Events.UserInterfaceEventSource.ControlCreatedEventArgs(addInOptionsPanel.OptionsGroupBox));
                    }
                    break;
            }
        }

        /// <summary>
        /// Handles the Click event of the okButton control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="T:System.EventArgs"/> instance containing the event data.</param>
        private void okButton_Click(object sender, EventArgs e)
        {
            UpdateOptions();
            // Do this 'cause running tests changes the directory for Rake.
            Environment.CurrentDirectory = Path.GetDirectoryName(Application.ExecutablePath);
            Options.Instance.SaveOptions(Options.Instance);
            this.DialogResult = DialogResult.OK;
        }

        /// <summary>
        /// Updates the options.
        /// </summary>
        private void UpdateOptions()
        {
            Options.Instance.RubyPath = generalOptionsPanel.RubyPath;
            Options.Instance.ServerPort = generalOptionsPanel.WebServerPort;
            Options.Instance.LineEndings = generalOptionsPanel.LineEndings;
            Options.Instance.LoadLastProjectAtStartUp = generalOptionsPanel.LoadLastProjectOnStartUp;
            Options.Instance.RunServer = generalOptionsPanel.RunScriptServer;
            Options.Instance.LaunchInternalBrowser = generalOptionsPanel.LaunchInternalBrowser;
            Options.Instance.ShowEOL = textEditorOptionsPanel.ShowEOL;
            Options.Instance.ShowSpaces = textEditorOptionsPanel.ShowSpaces;
            Options.Instance.ShowTabs = textEditorOptionsPanel.ShowTabs;
            Options.Instance.ShowLineNumbers = textEditorOptionsPanel.ShowLineNumbers;
			Options.Instance.ConvertTabsToSpaces = textEditorOptionsPanel.ConvertTabsToSpaces;
            Options.Instance.EnableFolding = textEditorOptionsPanel.EnableFolding;
            Options.Instance.ModelColor = textEditorOptionsPanel.ModelColor;
            Options.Instance.ViewColor = textEditorOptionsPanel.ViewColor;
            Options.Instance.ControllerColor = textEditorOptionsPanel.ControllerColor;
            Options.Instance.TestsColor = textEditorOptionsPanel.TestsColor;
            
            if (textEditorOptionsPanel.EditorFontFamily.Trim().Length > 0)
            {
                Options.Instance.EditorFont =
                    new Font(textEditorOptionsPanel.EditorFontFamily, textEditorOptionsPanel.EditorFontSize);
            }

            EditableHighlightService.SaveNewColors(highlightingOptionsPanel1.HightlitingEntites);
        }

    }
}